/*
** utils.c for  in /home/gomes_m//my-project-a-moi/client
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Mon Jul 29 16:03:49 2013 mickael gomes
** Last update Mon Jul 29 18:04:41 2013 mickael gomes
*/

#include		<stdlib.h>
#include		<struct.h>
#include		<prototypes.h>

char			*get_first_part(char *buff)
{
  char			*cmd;
  int			i = 0;

  cmd = xmalloc(1024);
  while (*buff && *buff != ' ')
    cmd[i++] = *(buff++);
  cmd[i] = 0;
  return (cmd);
}

char			*get_remains(char *buff)
{
  while (*buff && *buff != ' ')
    buff++;
  while (*buff == ' ')
    buff++;
  if (*buff == 0)
    return (NULL);
  return (buff);
}

int			get_how_many_chars(char *line)
{
  int			i;
  int			count;

  i = 0;
  count = 0;
  while (line[i])
    {
      if ((line[i] > ' ' || (line[i] == ' ' ||
			     line[i] == '\t')) && line[i + 1] > ' ')
	count++;
      i++;
    }
  return (count);
}

char			*treat_buff(char *cmd)
{
  char			*cleared_cmd;
  int			i = 0;
  int			j = 0;

  while (*cmd <= ' ' && *cmd)
    cmd++;
  if ((cleared_cmd = xmalloc(get_how_many_chars(cmd))) == NULL)
    return (NULL);
  while (cmd[i])
    {
      cleared_cmd[j++] = cmd[i++];
      while (cmd[i] <= ' ' && cmd[i] && cmd[i + 1] <= ' ')
	i++;
    }
  cleared_cmd[j] = 0;
  return (cleared_cmd);
}
